#pragma once
#include "InetAddr.hpp"
#include <string>
#include <functional>
#include <memory>

namespace EpollModule
{
    /**
     * 对 “连接”的描诉
     * 不管是普通fd，还是listenfd,我们都按照统一的方式处理
     */
    class Reactor;

    class Connection
    {
    protected:
        int _sockfd;
        std::string _inbuffer;  //连接的读缓冲区
        std::string _outbuffer; //连接的写缓冲区
        InetAddr _client_addr;    //连接所对应的客户端地址

        Reactor* _owner;  //连接属于哪个服务器,回指
        uint32_t _event;

    public:
        Connection()
            :_sockfd(-1)
            ,_event(0)
            ,_owner(nullptr)
        {
        }

        virtual void Sender() = 0;
        virtual void Recver() = 0;
        virtual void Excepter() = 0;

        Reactor* GetOwner()
        {
            return _owner;
        }

        void SetOwner(Reactor* epoll_server)
        {
            _owner = epoll_server;
        }
        

        //设置客户端信息
        void SetClientInfo(const InetAddr& client)
        {
            _client_addr = client;
        }

        void Close()
        {
            if(_sockfd > 0)
                close(_sockfd);
        }

        //设置/获取fd
        int GetSocketFd()
        {
            return _sockfd;
        }

        void SetSocketFd(int sockfd)
        {
            _sockfd = sockfd;
        }

        //添加/获取关心的事件
        void SetEvent(uint32_t event)
        {
            _event = event;
        }
        uint32_t GetEvent()
        {
            return _event;
        }

        bool OutBufferIsEmpty()
        {
            return _outbuffer.empty();
        }

        void InputBufAppend(const char* buf)
        {
            _inbuffer += buf;
        }

        void OuputBufAppend(const char* buf)
        {
            _outbuffer += buf;
        }

        void RemoveFromOutBuffer(int n)
        {
            _outbuffer.erase(0,n);
        }

        ~Connection()
        {}
        
    };
}